﻿function DateSelector_OnKeyPress(sender, e) {
	var keynum;
	if (window.event) { // IE
		keynum = e.keyCode;
	} else if (e.which) { // Other
		keynum = e.which;
	}
	var keychar = String.fromCharCode(keynum);
	var numcheck = /\d/;
	return numcheck.test(keychar);
}
function DateSelector_OnPropertyChange(sender, maxDigit) {
	if (sender.id.toString().length = maxDigit) {
	}	
}
function DateSelector_OnBlur(sender) {
	sender.value = DateSelector_Trim(sender.value);
	if (isNaN(sender.value)) {
		alert("숫자만 입력하십시오.");
		sender.focus();
		sender.select();
	}
	//sender.value = DateSelector_RemoveNoNumber();
	DateSelector_Invalidate(DateSelector_GetControl(sender));	
}
function DateSelector_Invalidate(control) {
	if (control == null) throw new "Null agrument value is not allowed.";
	var yearControl = document.getElementById(control.id + "_y");
	var monthControl = document.getElementById(control.id + "_m");
	var dayControl = document.getElementById(control.id + "_d");

	yearControl.value = DateSelector_MakeFourDigit(yearControl.value);
	monthControl.value = DateSelector_MakeTwoDigit(monthControl.value);
	dayControl.value = DateSelector_MakeTwoDigit(dayControl.value);
	
	control.value = yearControl.value + monthControl.value + dayControl.value;
}
function DateSelector_GetControl(childControl) {
	if (childControl == null) throw "Null argument value is not allowed.";
	var ret = document.getElementById(childControl.id.substring(0, childControl.id.length - 2));
	if (ret == null) throw "Cannot find control";
	return ret;
}
function DateSelector_MakeTwoDigit(value) {
	if (value == null) return;
	var strValue = new String(value);
	return (value.length == 1) ? "0" + strValue : strValue;
}

function DateSelector_MakeFourDigit(value) {
	if (value == null) return;
	var strValue = new String(value);
	if (value.length == 1) { return "000" + strValue; }
	if (value.length == 2) { return "00" + strValue; }
	if (value.length == 3) { return "0" + strValue; }
	return strValue;
}
function DateSelector_RemoveNoNumber(n) {
	n = n.toString();
	n = n.replace(/[^\d]/g, "");
	return n;
}
function DateSelector_Trim(s) {
	var m = s.match(/^\s*(\S+(\s+\S+)*)\s*$/);
	return (m == null) ? "" : m[1];
}
function DateSelector_Validate(controlID) {
	var control = document.getElementById(controlID);
	if (control.value.length != 8) return false;

	var year = new Number(control.value.substring(0, 4));
	var month = new Number(control.value.substring(4, 6)) - 1;
	var day = new Number(control.value.substring(6, 8));
	var tempDate = new Date(year,month,day);
	alert(year + "-" + month + "-" + day);
	alert(tempDate.getFullYear() + "-" + tempDate.getMonth() + "-" + tempDate.getDate());
	return (tempDate.getFullYear() == year) &&
		(month == tempDate.getMonth()) &&
		(day == tempDate.getDate());
}